package cn.yuanqiao.web.controller.archive.guidang;

import cn.yuanqiao.archive.appManager.domain.TenantYqdaDocument;
import cn.yuanqiao.archive.guidang.domain.Guidangpackage;
import cn.yuanqiao.archive.guidang.service.GuidangService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import cn.yuanqiao.common.core.context.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/archive/guidang")
public class GuiDangController extends BaseController {
    @Autowired
    private GuidangService guidangService;

    @GetMapping("/getInfo")
    public AjaxResult list(@RequestParam("folderId") String folderId, @RequestParam("msg") String msg,@RequestParam("bhrId") String bhrId)
    {
        List<Map<String, Object>> dsrxx = guidangService.getDsrxx(msg, folderId,bhrId);
        return success(dsrxx);
    }

    @GetMapping("/guiDangTimer")
    public void guiDangTimer(@RequestParam("tenantCode") String tenantCode)
    {
        guidangService.guiDangTimer(tenantCode);
    }

    @PreAuthorize("@ss.hasPermi('archive:guidangpackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(Guidangpackage a001Guidangpackage)
    {
        startPage();
        List<Guidangpackage> list = guidangService.selectA001GuidangpackageList(a001Guidangpackage);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('archive:guidangpackage:remove')")
    @Log(title = "数据包删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{packageIds}")
    public AjaxResult remove(@PathVariable String[] packageIds)
    {
        return toAjax(guidangService.deleteA001GuidangpackageByPackageIds(packageIds));
    }


    @PostMapping("/viewPdf")
    public void viewPdf(@RequestParam("categoryId") Long categoryId,@RequestParam("folderId") Long folderId,@RequestParam("type")Integer type, HttpServletResponse response) throws Exception {
        //String path = SealmanageService.viewSeal(Sealmanage);
        TenantYqdaDocument document = guidangService.getFilePath(folderId, type, categoryId);
        String path = document.getDocMnt()+document.getPath();
        File file = new File(path);
        if(!file.exists()){
            throw new ServiceException("文件不存在");
        }
        response.reset(); // 非常重要
        // 设置 CORS 响应头
        ResponseUtils.defaultResponse(response);
        //在线预览，浏览器支持的文件类型都能打开
        //URL u = new URL("file:///" + path);
        //response.setContentType(u.openConnection().getContentType());
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "inline; filename=\"" + document.getDocName()+"\"");
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
        byte[] buf = new byte[1024];
        int len = 0;
        OutputStream out = response.getOutputStream();
        while ((len = br.read(buf)) > 0){
            out.write(buf, 0, len);
        }
        System.gc();
        br.close();
        out.close();
        //return success(SealmanageService.viewSeal(Sealmanage));
    }
}
